#include "icondesigndialog.h"
#include "ui_icondesigndialog.h"
#include <QFileDialog>
#include <QImage>
#include <QDir>
#include <QFile>
#include <QDebug>
#include <qmath.h>


IconDesignDialog::IconDesignDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::IconDesignDialog)
{
    ui->setupUi(this);
}

IconDesignDialog::~IconDesignDialog()
{
    delete ui;
}

void IconDesignDialog::on_pushButton_load_clicked()
{
    QStringList sFilePath = QFileDialog::getOpenFileNames(this,tr("open file"),QCoreApplication::applicationDirPath(),"*.png");
    if(sFilePath.isEmpty())return;

    ui->listWidget->addItems(sFilePath);
}

void IconDesignDialog::on_pushButton_multiload_clicked()
{
    QString sDirPath = QFileDialog::getExistingDirectory(this,tr("load files"),QCoreApplication::applicationDirPath());
    if(sDirPath.isEmpty())return;
    loadChidlDir(sDirPath);
}

void IconDesignDialog::on_pushButton_trans_clicked()
{
    while (ui->listWidget->count() > 0) {
        QListWidgetItem* pitem = ui->listWidget->takeItem(0);
        QString filepath = pitem->data(0).toString();
        // 处理为黑白的
        translatePng(filepath);
        // 将黑色与白色颠倒
        //        swapBlackAndWhite(filepath);
        delete pitem;
        pitem = NULL;
    }
}

void IconDesignDialog::loadChidlDir(const QString &dirpath)
{
    QDir dir(dirpath);
    QStringList files = dir.entryList(QDir::Files);
    foreach (QString file, files) {
        if(file.right(4) != ".png")
        {
            continue;
        }
        else {
            ui->listWidget->addItem(dir.absoluteFilePath(file));
        }
    }

    QStringList dirs = dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot);
    foreach (QString dirName, dirs) {
        loadChidlDir(dir.absoluteFilePath(dirName));
    }
}

void IconDesignDialog::translatePng(const QString &filepath)
{
//    QDir dir(QCoreApplication::applicationDirPath());
//    dir.cdUp();
//    if(!dir.exists("resoutput"))dir.mkdir("resoutput");
//    dir.cd("resoutput");

    QImage image(filepath);
    for(int j=0; j<image.width(); ++j)
    {
        for(int i=0; i<image.height(); ++i)
        {
            QColor color = image.pixelColor(j,i);
            int r = color.red();
            int g = color.green();
            int b = color.blue();
            int a = color.alpha();
            int ave = qFloor((r+g+b)*0.33);
            if(a == 0)continue;

            // 认为是白色，设置为透明
            if(ave > 240){
                image.setPixelColor(j,i,QColor(r,g,b,0));
                continue;
            }
#if 0
            double xishu = (255.0-ave)/256;
            image.setPixelColor(j,i,QColor(qFloor(xishu*63),qFloor(xishu*200),qFloor(xishu*255),a));
#else
            image.setPixelColor(j,i,QColor(63,200,255,a));
#endif
        }
    }

    QString outFilePath = filepath.section(".",0,0)+"-1."+filepath.section(".",1,1);
    image.save(outFilePath);
}

void IconDesignDialog::swapBlackAndWhite(const QString &filepath)
{
//    QDir dir(QCoreApplication::applicationDirPath());
//    dir.cdUp();
//    if(!dir.exists("resoutput"))dir.mkdir("resoutput");
//    dir.cd("resoutput");

    QImage image(filepath);

    for(int i=0; i<image.height(); ++i)
    {
        for(int j=0; j<image.width(); ++j)
        {
            QColor color = image.pixelColor(i,j);
            int r = color.red();
            int g = color.green();
            int b = color.blue();
            int a = color.alpha();
            image.setPixelColor(i,j,QColor(0xff-r,0xff-g,0xff-b,a));
        }
    }
    QString outFilePath = filepath.section(".",0,0)+"-1"+filepath.section(".",1,1);
    image.save(outFilePath);
//    image.save(dir.absoluteFilePath(filepath.section("/",-1,-1)));
}
